public class LoanCalculator {
    private int a;  // 贷款总额(万元)
    private int m;  // 贷款时间(月数)
    private double y;   // 贷款年利率
    private double avgInterest;
    private double avgCap;

    public LoanCalculator(int a, int m, double y) {
        this.a = a;
        this.m = m;
        this.y = y;
    }

    // 等额本金还款法
    public void calculateAvgInterest() {
        avgInterest = (m+1) * a * (y/12) / 2;
        print(1);
    }

    // 等额本息还款法
    public void calculateAvgCap() {
        double x = Math.pow(1+(y/12), m);
        avgCap = m * a * (y/12) * x / (x-1) - a;
        print(2);
    }

    private void print(int type) {
        if(type == 1)
            System.out.println("用等额本金还款法,您需要支付的总利息为:" + avgInterest);
        if(type == 2)
            System.out.println("用等额本息还款法,您需要支付的总利息为:" + avgCap);
    }
}
